package com.wwj.auction.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.admin.entity.GetOut;
import com.wwj.auction.admin.entity.Store;
import com.wwj.auction.admin.entity.UserInfo;
import com.wwj.auction.admin.entity.vo.PageModel;
import com.wwj.auction.admin.entity.vo.StoreTempVo;
import com.wwj.auction.admin.entity.vo.StoreVo;
import com.wwj.auction.admin.enums.StoreStatus;
import com.wwj.auction.admin.mapper.GetOutMapper;
import com.wwj.auction.admin.mapper.StoreMapper;
import com.wwj.auction.admin.mapper.UserInfoMapper;
import com.wwj.auction.admin.service.SafeScanService;
import com.wwj.auction.admin.service.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-25
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    @Resource
    private GetOutMapper getOutMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private SafeScanService safeScanService;

    @Override
    public StoreVo getStoreInfo(Integer id) {
        Store store = baseMapper.selectOne(
                Wrappers.lambdaQuery(Store.class)
                        .eq(Store::getUserId, id));
        StoreVo storeVo = new StoreVo();
        BeanUtils.copyProperties(store, storeVo);
        // 得到店铺id
        Integer storeId = store.getId();
        // 通过店铺id查询违规记录
        List<GetOut> getOutList = getOutMapper.selectList(
                Wrappers.lambdaQuery(GetOut.class)
                        .eq(GetOut::getStoreId, storeId));
        storeVo.setGetOut(getOutList.size());
        return storeVo;
    }

    @Override
    public PageModel<StoreTempVo> listForStore(Integer page, Integer limit) {
        // 扫描店铺违规记录
        safeScanService.getOutToSeized();

        // 分页查询所有店铺信息
        QueryWrapper<Store> emptyWrapper = Wrappers.emptyWrapper();
        Page<Store> storePage = new Page<>(page, limit);
        List<Store> storeList = baseMapper.selectPage(storePage, emptyWrapper).getRecords();
        // 封装Vo数据
        List<StoreTempVo> storeTemoVoList = storeList.stream().map(store -> {
            StoreTempVo storeTempVo = new StoreTempVo();
            storeTempVo.setId(store.getId());
            storeTempVo.setStoreName(store.getStoreName());
            // 查询店铺负责人信息
            Integer userId = store.getUserId();
            // 设置店铺评分
            storeTempVo.setScore(store.getServiceScore());
            // 设置违规记录数
            // 通过店铺id查询每个商品的评分
            List<GetOut> getOutList = getOutMapper.selectList(Wrappers.lambdaQuery(GetOut.class)
                    .eq(GetOut::getStoreId, store.getId()));
            // 遍历商品评分集合，统计所有违规记录
            long count = getOutList.stream().filter(getOut -> {
                return getOut.getScore() < 6;
            }).count();
            storeTempVo.setGetOut((int) count);
            // 更新一下店铺的违规记录
            store.setGetOut((int) count);
            baseMapper.updateById(store);
            UserInfo userInfo = userInfoMapper.selectById(userId);
            if (userInfo != null) {
                storeTempVo.setName(userInfo.getName());
                storeTempVo.setPhone(userInfo.getPhone());
                storeTempVo.setIdCard(userInfo.getIdCard());
                // 设置店铺状态
                storeTempVo.setStatus(userInfo.getStoreStatus());
            }
            return storeTempVo;
        }).collect(Collectors.toList());
        // 封装最终结果
        PageModel<StoreTempVo> pageModel = new PageModel<>();
        pageModel.setList(storeTemoVoList);
        pageModel.setTotal(storePage.getTotal());
        return pageModel;
    }

    @Override
    public void status(Integer storeId, Integer status) {
        // 查询店铺信息
        Store store = baseMapper.selectById(storeId);
        // 获取店铺的负责人id
        Integer userId = store.getUserId();
        // 通过用户id查询用户信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        // 设置店铺状态
        if (status.equals(StoreStatus.IS_SEALED.getStatus())) {
            userInfo.setStoreStatus(StoreStatus.IS_SEALED.getStatus());
        } else if (status.equals(StoreStatus.NOT_OPENING.getStatus())) {
            userInfo.setStoreStatus(StoreStatus.NOT_OPENING.getStatus());
        }
        // 更新
        userInfoMapper.updateById(userInfo);
    }
}
